﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.AvalonDock.Layout;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using Xceed.Wpf.AvalonDock.Layout.Serialization;
using System.Resources;
using System.Collections;
using Xceed.Wpf.AvalonDock;

namespace Devpad_IDE
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private bool IsProjectOpened = false;
        private string ProjectName = null;
        private string ProjectPath = null;

    #region "Reusable Code"

        private void SaveDockLayout()
        {
            var serializer = new Xceed.Wpf.AvalonDock.Layout.Serialization.XmlLayoutSerializer(dockingManager);
            using (var stream = new StreamWriter("Layout.xml"))
                serializer.Serialize(stream);
        }

        private void LoadDockLayout()
        {
            if (File.Exists("Layout.xml"))
            {
                var serializer = new Xceed.Wpf.AvalonDock.Layout.Serialization.XmlLayoutSerializer(dockingManager);
                using (var stream = new StreamReader("Layout.xml"))
                    serializer.Deserialize(stream);
            }
            //var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "ProjectPanel");
            //ProjectPanelMenuItem.IsChecked = layout.IsVisible;
        }

        private void UpdateUILayout()
        {
            if (MainMenu.Visibility == System.Windows.Visibility.Visible && MainToolbar.Visibility == System.Windows.Visibility.Visible && Statusbar.Visibility == System.Windows.Visibility.Visible)
            {
                MainToolbar.Margin = new Thickness(0, 21, 0, 0);
                dockingManager.Margin = new Thickness(0, 48, 0, 23);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Collapsed && MainToolbar.Visibility == System.Windows.Visibility.Visible && Statusbar.Visibility == System.Windows.Visibility.Visible)
            {
                MainToolbar.Margin = new Thickness(0, 0, 0, 0);
                dockingManager.Margin = new Thickness(0, 27, 0, 23);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Visible && MainToolbar.Visibility == System.Windows.Visibility.Collapsed && Statusbar.Visibility == System.Windows.Visibility.Visible)
            {
                dockingManager.Margin = new Thickness(0, 21, 0, 23);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Visible && MainToolbar.Visibility == System.Windows.Visibility.Visible && Statusbar.Visibility == System.Windows.Visibility.Collapsed)
            {
                dockingManager.Margin = new Thickness(0, 48, 0, 0);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Visible && MainToolbar.Visibility == System.Windows.Visibility.Collapsed && Statusbar.Visibility == System.Windows.Visibility.Collapsed)
            {
                dockingManager.Margin = new Thickness(0, 21, 0, 0);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Collapsed && MainToolbar.Visibility == System.Windows.Visibility.Visible && Statusbar.Visibility == System.Windows.Visibility.Collapsed)
            {
                MainToolbar.Margin = new Thickness(0, 0, 0, 0);
                dockingManager.Margin = new Thickness(0, 27, 0, 0);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Collapsed && MainToolbar.Visibility == System.Windows.Visibility.Collapsed && Statusbar.Visibility == System.Windows.Visibility.Visible)
            {
                dockingManager.Margin = new Thickness(0, 0, 0, 23);
            }
            else if (MainMenu.Visibility == System.Windows.Visibility.Collapsed && MainToolbar.Visibility == System.Windows.Visibility.Collapsed && Statusbar.Visibility == System.Windows.Visibility.Collapsed)
            {
                dockingManager.Margin = new Thickness(0, 0, 0, 0);
            }
        }

    #endregion

    #region "MainWindow"

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDockLayout();

            if (Properties.Settings.Default.MainWindow_IsMax)
            {
                WindowState = System.Windows.WindowState.Maximized;
            }

            if (Properties.Settings.Default.Options_Theme == 1)
            {
                dockingManager.Theme = new Xceed.Wpf.AvalonDock.Themes.AeroTheme();
            }
            Background = dockingManager.Background;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            if (WindowState == System.Windows.WindowState.Maximized)
            {
                Properties.Settings.Default.MainWindow_IsMax = true;
            }
            else
            {
                Properties.Settings.Default.MainWindow_IsMax = false;
            }
            SaveDockLayout();
            Devpad_IDE.Properties.Settings.Default.Save();
            Application.Current.Shutdown();
        }

    #endregion

    #region "MainMenu"

        private void NewProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Devpad_IDE.Dialogs.File.NewProjectDialog dialog = new Devpad_IDE.Dialogs.File.NewProjectDialog(dockingManager.Background);
            dialog.Owner = this;
            dialog.ShowDialog();
            if (dialog.Result == "OK")
            {
                //TODO: create new project

            }
        }

        private void NewFileMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Devpad_IDE.Dialogs.File.NewFileDialog dialog = new Devpad_IDE.Dialogs.File.NewFileDialog(dockingManager.Background);
            dialog.Owner = this;
            dialog.ShowDialog();
            if (dialog.Result == "OK")
            {
                //TODO: create new project
            }
        }

        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

    #region "View"

        private void MenubarMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (MenubarMenuItem.IsChecked)
            {
                MainMenu.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                MainMenu.Visibility = System.Windows.Visibility.Collapsed;
            }
            UpdateUILayout();
        }

        private void ToolbarMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (ToolbarMenuItem.IsChecked)
            {
                MainToolbar.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                MainToolbar.Visibility = System.Windows.Visibility.Collapsed;
            }
            UpdateUILayout();
        }

        private void StatusbarMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (StatusbarMenuItem.IsChecked)
            {
                Statusbar.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                Statusbar.Visibility = System.Windows.Visibility.Collapsed;
            }
            UpdateUILayout();
        }

        private void ProjectPanelMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "ProjectPanel");
            if (layout.IsHidden)
                layout.Show();
            else if (layout.IsVisible)
                layout.IsActive = true;
            else
                ProjectPanelLayout.AddToLayout(dockingManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
        }

        private void PropertiesMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "PropertiesPanel");
            if (layout.IsHidden)
                layout.Show();
            else if (layout.IsVisible)
                layout.IsActive = true;
            else
                PropertiesPanelLayout.AddToLayout(dockingManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
        }

        private void CommandMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "CommandPanel");
            if (layout.IsHidden)
                layout.Show();
            else if (layout.IsVisible)
                layout.IsActive = true;
            else
                CommandPanelLayout.AddToLayout(dockingManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
        }

        private void OutputMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "OutputPanel");
            if (layout.IsHidden)
                layout.Show();
            else if (layout.IsVisible)
                layout.IsActive = true;
            else
                OutputPanelLayout.AddToLayout(dockingManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
        }

        private void ErrorListMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "ErrorListPanel");
            if (layout.IsHidden)
                layout.Show();
            else if (layout.IsVisible)
                layout.IsActive = true;
            else
                ErrorListPanelLayout.AddToLayout(dockingManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
        }

        private void TaskListMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var layout = dockingManager.Layout.Descendents().OfType<Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable>().Single(a => a.ContentId == "TaskListPanel");
            if (layout.IsHidden)
                layout.Show();
            else if (layout.IsVisible)
                layout.IsActive = true;
            else
                TaskListPanelLayout.AddToLayout(dockingManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
        }

    #endregion

        private void OptionsMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Devpad_IDE.Dialogs.Tools.OptionsDialog dialog = new Devpad_IDE.Dialogs.Tools.OptionsDialog(dockingManager.Background);
            dialog.Owner = this;
            dialog.ShowDialog();
        }

        private void AboutMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Devpad_IDE.Dialogs.Help.AboutDialog dialog = new Devpad_IDE.Dialogs.Help.AboutDialog();
            dialog.Owner = this;
            dialog.ShowDialog();
        }

    #endregion

    }
}